# -*- coding: utf-8 -*-

# @Time : 2022/6/12 21:06

# @Author : scy

# @File : __init__.py.py

# @Software: PyCharm
import random

import interact


class user:

    # 用来初始化
    def __init__(self):
        self.__fit_constellation = None
        self.__staff = None
        self.__good_impression3 = None
        self.__good_impression2 = None
        self.__good_impression1 = None
        self.__phone_number = None
        self.__password = None
        self.__education = None
        self.__constellation = None
        self.__age_of_birth = None
        self.__photo = None
        self.__dating = None
        self.__own_state = None
        self.__hobby = None
        self.__demand = None
        self.__record = None
        self.__weight = None
        self.__height = None
        self.__number = None
        self.__name = None
        self.__sex = None

    # 用来用户注册
    def register(self, cphone_number, cname, csex, cpassword, repeat_password, cheight, cweight, ceducation, chobby,
                 cdemand, cphoto, cage_of_birth):

        # 判断密码是否符合要求
        if len(cpassword) < 8 or len(cpassword) > 16:
            return '密码长度不符合要求'
        password_flag = False
        for pad in cpassword:
            if not (('a' <= pad <= 'z') or ('A' <= pad <= 'Z') or ('0' <= pad <= '9')):
                password_flag = True
                break
        if password_flag:
            return '密码不符合要求，密码必须是字母或者是数字'

        if cpassword != repeat_password:
            return '两次密码不同'

        # 进行用户信息保存
        self.__phone_number = cphone_number
        self.__name = cname
        self.__sex = csex
        self.__password = cpassword
        self.__height = cheight
        self.__weight = cweight
        self.__education = ceducation
        self.__hobby = chobby
        self.__demand = cdemand
        self.__own_state = '单身'
        self.__good_impression1 = None
        self.__good_impression2 = None
        self.__good_impression3 = None
        self.__dating = None
        # 待确定如何利用
        self.__photo = cphoto
        self.__age_of_birth = cage_of_birth

        # 存储星座信息
        constellations = [[321, 412, '白羊座'], [420, 520, '金牛座'], [521, 621, '双子座'], [622, 722, '巨蟹座'], [723, 822, '狮子座'],
                          [823, 922, '处女座'], [923, 1023, '天秤座'], [1024, 1124, '天蝎座'], [1123, 1231, '射手座'],
                          [1222, 1231, '魔羯座'], [100, 119, '魔羯座'], [120, 218, '水瓶座'], [219, 320, '双鱼座']]

        # 出生年月形成的数字
        birth_num = (int(cage_of_birth[5]) - int('0')) * 1000 + (int(cage_of_birth[6]) - int('0')) * 100 + (
                int(cage_of_birth[8]) - int('0')) * 10 + (int(
            cage_of_birth[9]) - int('0'))
        for col in constellations:
            if col[0] <= birth_num <= col[1]:
                self.__constellation = col[2]
                break

        # 契合星座列表
        fit = {'白羊座': ['狮子座', '射手座'], '金牛座': ['处女座', '魔羯座', '天秤座'], '双子座': ['天秤座', '水瓶座', '狮子座'],
               '巨蟹座': ['水瓶座', '巨蟹座', '天蝎座'], '狮子座': ['白羊座', '射手座'], '处女座': ['摩羯座', '金牛座'], '天秤座': ['双子座', '水瓶座'],
               '天蝎座': ['巨蟹座', '双鱼座', '魔羯座', '金牛座'], '射手座': ['白羊座', '狮子座', '双子座'], '摩羯座': ['金牛座', '处女座', '巨蟹座'],
               '水瓶座': ['天秤座', '双子座', '白羊座', '射手座', '狮子座'], '双鱼座': ['巨蟹座', '天蝎座']}
        self.__fit_constellation = fit[self.__constellation]
        # 向数据库中插入数据
        try:
            conn = interact.interactSql()
            conn.connect_sql()
            conn.cursor_sql()
            conn.run_sql('insert into user(phone_number, sname, sex, password, height, weight, education, hobby,'
                         'demand, '
                         'own_state,good_impression1, good_impression2, good_impression3, dating, photo, '
                         'age_of_birth, constellation) values ("%s","%s","%s", '
                         '"%s","%s","%s","%s","%s","%s", '
                         '"%s","%s","%s","%s","%s","%s","%s","%s")' % (self.__phone_number, self.__name,
                                                                       self.__sex, self.__password,
                                                                       self.__height, self.__weight,
                                                                       self.__education,
                                                                       self.__hobby, self.__demand,
                                                                       self.__own_state,
                                                                       self.__good_impression1, self.__good_impression2,
                                                                       self.__good_impression3,
                                                                       self.__dating, self.__photo,
                                                                       self.__age_of_birth, self.__constellation))
            conn.commit()
            return '创建成功，即将跳转到登陆成功界面'
        except Exception as e:
            if 'conn' in dir():
                print(e)
            print(e)
            return '创建错误，请联系工作人员'
        finally:
            if 'conn' in dir():
                conn.close_database('cursor')
                conn.close_database('connect')

    # 用户登录
    def login(self, cphone_number, cpassword):

        self.__phone_number = cphone_number
        self.__password = cpassword

        # 从数据库中取出数据查询是否能登录
        try:
            conn = interact.interactSql()
            conn.connect_sql()
            conn.cursor_sql()
            conn.run_sql('select * from user where phone_number = "%s"' % self.__phone_number)
            user_message = conn.get_results()
            if len(user_message) == 0:
                return '用户不存在'
            # 判断密码是否相同
            if user_message[0][3] == cpassword:
                # 赋值
                (self.__phone_number, self.__name,
                 self.__sex, self.__password,
                 self.__height, self.__weight, self.__age_of_birth, self.__constellation,
                 self.__education,
                 self.__hobby, self.__demand,
                 self.__own_state,
                 self.__good_impression1, self.__good_impression2, self.__good_impression3,
                 self.__dating, self.__photo, self.__staff
                 ) = user_message[0]

                # 契合星座列表
                fit = {'白羊座': ['狮子座', '射手座'], '金牛座': ['处女座', '魔羯座', '天秤座'], '双子座': ['天秤座', '水瓶座', '狮子座'],
                       '巨蟹座': ['水瓶座', '巨蟹座', '天蝎座'], '狮子座': ['白羊座', '射手座'], '处女座': ['摩羯座', '金牛座'],
                       '天秤座': ['双子座', '水瓶座'],
                       '天蝎座': ['巨蟹座', '双鱼座', '魔羯座', '金牛座'], '射手座': ['白羊座', '狮子座', '双子座'], '摩羯座': ['金牛座', '处女座', '巨蟹座'],
                       '水瓶座': ['天秤座', '双子座', '白羊座', '射手座', '狮子座'], '双鱼座': ['巨蟹座', '天蝎座']}
                self.__fit_constellation = fit[self.__constellation]
                return '登陆成功'
            else:
                return '密码错误，请重新输入'
        except Exception as e:
            if 'conn' in dir():
                print(e)
            return '错误，请联系工作人员'
        finally:
            if 'conn' in dir():
                conn.close_database('cursor')
                conn.close_database('connect')

    # 随机生成相亲对象
    def random_love(self):
        # 性别转换

        sex_shift = {'男': '女', '女': '男'}
        love_sex = sex_shift[self.__sex]

        # 从数据库中取出相亲用户
        try:
            conn = interact.interactSql()
            conn.connect_sql()
            conn.cursor_sql()
            conn.run_sql('select * from user where sex ="%s" and own_state = "单身"' % love_sex)
            # 转化为list， 否则不能乱序
            user_message = list(conn.get_results())
            # 打乱输出
            random.shuffle(user_message)
            return user_message  # 暂时按序输出

        except Exception as e:
            if 'conn' in dir():
                print(e)
            return '错误，请联系工作人员'
        finally:
            if 'conn' in dir():
                conn.close_database('cursor')
                conn.close_database('connect')

    # 修改用户信息, 暂支持修改手机号 不含修改密码
    def modify_information(self, cname='', csex='', cheight='', cweight='', ceducation='', chobby='',
                           cdemand='', cown_state='',
                           cgood_impression1='', cgood_impression2='', cgood_impression3='', cdating='', cphoto='',
                           cage_of_birth=''):
        if cname != '':
            self.__name = cname
        if csex != '':
            self.__sex = csex
        if cheight != '':
            self.__height = cheight
        if cweight != '':
            self.__weight = cweight
        if ceducation != '':
            self.__education = ceducation
        if chobby != '':
            self.__hobby = chobby
        if cdemand != '':
            self.__demand = cdemand
        if cown_state != '':
            self.__own_state = cown_state
        if cgood_impression1 != '':
            self.__good_impression1 = cgood_impression1
        if cgood_impression2 != '':
            self.__good_impression2 = cgood_impression2
        if cgood_impression3 != '':
            self.__good_impression3 = cgood_impression3
        if cdating != '':
            self.__dating = cdating
        if cphoto != '':
            self.__photo = cphoto
        if cage_of_birth != '':
            self.__age_of_birth = cage_of_birth
            # 存储星座信息
            constellations = [[321, 412, '白羊座'], [420, 520, '金牛座'], [521, 621, '双子座'], [622, 722, '巨蟹座'],
                              [723, 822, '狮子座'],
                              [823, 922, '处女座'], [923, 1023, '天秤座'], [1024, 1124, '天蝎座'], [1123, 1231, '射手座'],
                              [1222, 1231, '魔羯座'], [100, 119, '魔羯座'], [120, 218, '水瓶座'], [219, 320, '双鱼座']]

            # 出生年月形成的数字
            birth_num = (int(cage_of_birth[5]) - int('0')) * 1000 + (int(cage_of_birth[6]) - int('0')) * 100 + (
                    int(cage_of_birth[8]) - int('0')) * 10 + (int(
                cage_of_birth[9]) - int('0'))
            for col in constellations:
                if col[0] <= birth_num <= col[1]:
                    self.__constellation = col[2]
                    break
                    # 契合星座列表
            fit = {'白羊座': ['狮子座', '射手座'], '金牛座': ['处女座', '魔羯座', '天秤座'], '双子座': ['天秤座', '水瓶座', '狮子座'],
                   '巨蟹座': ['水瓶座', '巨蟹座', '天蝎座'], '狮子座': ['白羊座', '射手座'], '处女座': ['摩羯座', '金牛座'],
                   '天秤座': ['双子座', '水瓶座'],
                   '天蝎座': ['巨蟹座', '双鱼座', '魔羯座', '金牛座'], '射手座': ['白羊座', '狮子座', '双子座'],
                   '摩羯座': ['金牛座', '处女座', '巨蟹座'],
                   '水瓶座': ['天秤座', '双子座', '白羊座', '射手座', '狮子座'], '双鱼座': ['巨蟹座', '天蝎座']}
            self.__fit_constellation = fit[self.__constellation]

        # 向数据库中修改数据
        try:
            conn = interact.interactSql()
            conn.connect_sql()
            conn.cursor_sql()
            conn.run_sql(
                'update  user set sname = "%s", sex = "%s", height = "%s", weight = "%s", education = "%s", '
                'hobby = "%s", '
                'demand = "%s", '
                'own_state = "%s",good_impression1 = "%s",good_impression2 = "%s",good_impression3 = "%s", '
                'dating = "%s", '
                'photo = "%s", age_of_birth = "%s", constellation = "%s" '
                'where phone_number = "%s"' % (
                    self.__name,
                    self.__sex,
                    self.__height, self.__weight,
                    self.__education,
                    self.__hobby, self.__demand,
                    self.__own_state,
                    self.__good_impression1, self.__good_impression2, self.__good_impression3,
                    self.__dating, self.__photo,
                    self.__age_of_birth, self.__constellation, self.__phone_number))
            conn.commit()
            return '修改成功'
        except Exception as e:
            if 'conn' in dir():
                conn.rollback()
                print(e)
            print(e)
            return '错误，请联系工作人员'
        finally:
            if 'conn' in dir():
                conn.close_database('cursor')
                conn.close_database('connect')

    # 修改密码
    def modify_password(self, old_password, new_password, repeat_password):
        if old_password != self.__password:
            return '原密码错误'

        if new_password != repeat_password:
            return '两次密码不相同，请重新输入'

        if len(new_password) < 8 or len(new_password) > 16:
            return '密码长度不符合要求'
        password_flag = False
        for pad in new_password:
            if not (('a' <= pad <= 'z') or ('A' <= pad <= 'Z') or ('0' <= pad <= '9')):
                password_flag = True
                break
        if password_flag:
            return '密码不符合要求，密码必须是字母或者是数字'

        # 向数据库中修改密码
        try:
            conn = interact.interactSql()
            conn.connect_sql()
            conn.cursor_sql()
            conn.run_sql(
                'update user set password = "%s" '
                'where phone_number = %s' % (
                    new_password, self.__phone_number))
            self.__password = new_password
            conn.commit()
            return '密码修改成功'
        except Exception as e:
            if 'conn' in dir():
                conn.rollback()
                print(e)
            print(e)
            return '错误，请联系工作人员'
        finally:
            if 'conn' in dir():
                conn.close_database('cursor')
                conn.close_database('connect')

    # 添加用户好感人
    def add_good_impression(self, good_phone_number):

        if self.__own_state == '牵手成功':
            return '都已经牵手成功了，做个人吧'
        if self.__dating is not None:
            return '已有约会人，请不要做渣男'
        try:
            if self.__good_impression3 is not None and self.__good_impression2 is not None and self.__good_impression3 is not None:
                return '已有三个好感人，无法添加，如果还需添加，请删除几个好感人'

            if self.__good_impression1 is not None:
                conn = interact.interactSql()
                conn.connect_sql()
                conn.cursor_sql()
                conn.run_sql(
                    'update  user set good_impression1 = "%s" '
                    'where phone_number = "%s"' % (
                        good_phone_number, self.__phone_number))
                self.__good_impression1 = good_phone_number
                conn.commit()
                return '好感人加入成功'
            elif self.__good_impression2 is not None:
                conn = interact.interactSql()
                conn.connect_sql()
                conn.cursor_sql()
                conn.run_sql(
                    'update  user set good_impression2 = "%s" '
                    'where phone_number = "%s"' % (
                        good_phone_number, self.__phone_number))
                self.__good_impression2 = good_phone_number
                conn.commit()
                return '好感人加入成功'
            else:
                conn = interact.interactSql()
                conn.commit()
                conn.cursor_sql()
                conn.run_sql(
                    'update  user set good_impression3 = "%s" '
                    'where phone_number = "%s"' % (
                        good_phone_number, self.__phone_number))
                self.__good_impression3 = good_phone_number
                conn.commit()
                return '好感人加入成功'
        except Exception as e:
            if 'conn' in dir():
                conn.rollback()
                print(e)
            print(e)
            return '错误，请联系工作人员'
        finally:
            if 'conn' in dir():
                conn.close_database('cursor')
                conn.close_database('connect')

    # 请求约会
    def ask_dating(self, dating_phone_number):
        if self.__own_state == '牵手成功':
            return '都已经牵手成功了，做个人吧'
        if self.__dating is not None:
            return "已有约会人，无法添加"
        if dating_phone_number != self.__good_impression2 and dating_phone_number != self.__good_impression1 and dating_phone_number != self.__good_impression3:
            return '请求约会人不在好感人列表'

        # 从数据库中取出约会人数据
        try:
            conn = interact.interactSql()
            conn.connect_sql()
            conn.cursor_sql()
            conn.run_sql('select * from user where phone_number = "%s"' % dating_phone_number)
            user_message = conn.get_results()
            if len(user_message) == 0:
                return '用户不存在'
            # 判断对方对他是否有好感
            if user_message[0][12] != self.__phone_number and user_message[0][
                13] != self.__phone_number and user_message[0][14] != self.__phone_number:
                return '很遗憾，对方对你没有好感'

            self.modify_information(cdating=dating_phone_number)
            # 对方约会人加入
            conn.run_sql(
                'update user set dating = "%s" where phone_number = "%s"' % (self.__phone_number, dating_phone_number))
            conn.commit()
            return '可以约会,请你选择约会地点，时间等信息'
        except Exception as e:
            if 'conn' in dir():
                conn.rollback()
                print(e)
            return '错误，请联系工作人员'
        finally:
            if 'conn' in dir():
                conn.close_database('cursor')
                conn.close_database('connect')

    # 添加一条记录
    def add_record(self, dating_phone_number, dating_time, place):
        try:
            conn = interact.interactSql()
            conn.connect_sql()
            conn.cursor_sql()
            conn.run_sql('select * from place where location = "%s"' % place)
            place_message = conn.get_results()
            if not place_message:
                return '地点信息不存在'
            if place_message[0][2] >= 5:
                return '该地点人数过多，请重新选择地点'
            # 判断性别
            if self.__sex == '男':
                nanuser = self.__phone_number
                nvuser = dating_phone_number
            else:
                nanuser = dating_phone_number
                nvuser = self.__phone_number
            # 插入数据
            conn.run_sql('insert into record(time, place, staff, nanuser,nvuser) values("%s","%s","%s","%s","%s")' % (
                dating_time, place, place_message[0][1], nanuser, nvuser))
            # 修改地点人数
            conn.run_sql('update place set people_sum = %d' % (place_message[0][2] + 1))
            conn.commit()
            return '约会地点增加成功，记得评论满意程度'
        except Exception as e:
            if 'conn' in dir():
                conn.rollback()
                print(e)
            return '错误，请联系工作人员'
        finally:
            if 'conn' in dir():
                conn.close_database('cursor')
                conn.close_database('connect')

    # 读取所有约会地点
    def all_dating_record(self):
        try:
            conn = interact.interactSql()
            conn.connect_sql()
            conn.cursor_sql()
            if self.__sex == '男':
                conn.run_sql('select * from record where nanuser = "%s"' % self.__phone_number)
                return conn.get_results()
            else:
                conn.run_sql('select * from record where nvuser = "%s"' % self.__phone_number)
                return conn.get_results()
        except Exception as e:
            if 'conn' in dir():
                print(e)
            return '错误，请联系工作人员'
        finally:
            if 'conn' in dir():
                conn.close_database('cursor')
                conn.close_database('connect')

    # 对约会人进行评分
    def score(self, dating_phone_dating, satisfaction):
        try:
            conn = interact.interactSql()
            conn.connect_sql()
            conn.cursor_sql()

            # 判断性别
            if self.__sex == '男':
                conn.run_sql('select * from record where nanuser = "%s" and nvuser = "%s"' % (
                    self.__phone_number, dating_phone_dating))
                record_message = conn.get_results()
                # 如果对方已经评价过
                if record_message[0][7] is not None:
                    # 双方都满意 加入数据库 修改双方状态
                    if satisfaction == '满意' and record_message[0][7] == '满意':
                        conn.run_sql(
                            'update record set satisfaction_nan = "%s", satisfaction = "%s" where nanuser = "%s" and '
                            'nvuser '
                            '= "%s" ' % (
                                satisfaction, satisfaction, self.__phone_number, dating_phone_dating))
                        conn.run_sql(
                            'update user set owe_state = "牵手成功" where phone_number = "%s"' % dating_phone_dating)
                        self.modify_information(cown_state='牵手成功')
                        conn.commit()
                        return '恭喜你，牵手成功'
                    # 如果对方不满意
                    elif satisfaction == '满意' and record_message[0][7] != '满意':
                        conn.run_sql(
                            'update record set satisfaction_nan = "%s", satisfaction = "%s" where nanuser = "%s" and '
                            'nvuser '
                            '= "%s" ' % (
                                satisfaction, '不满意', self.__phone_number, dating_phone_dating))
                        conn.commit()
                        return '很遗憾，牵手失败'
                # 对方没评价
                else:
                    conn.run_sql(
                        'update record set satisfaction_nan = "%s" where nanuser = "%s" and nvuser = "%s" ' % (
                            satisfaction, self.__phone_number, dating_phone_dating))
                    conn.commit()
                    return '评价成功'
            else:
                conn.run_sql('select * from record where nanuser = "%s" and nvuser = "%s"' % (
                    dating_phone_dating, self.__phone_number))
                record_message = conn.get_results(0)
                # 如果对方已经评价过
                if record_message[0][6] is not None:
                    # 双方都满意 加入数据库 修改双方状态
                    if satisfaction == '满意' and record_message[0][6] == '满意':
                        conn.run_sql(
                            'update record set satisfaction_nv = "%s", satisfaction = "%s" where nanuser = "%s" and nvuser '
                            '= "%s" ' % (
                                satisfaction, satisfaction, dating_phone_dating, self.__phone_number))
                        conn.run_sql(
                            'update user set own_state = "牵手成功" where phone_number = "%s"' % dating_phone_dating)
                        self.modify_information(cown_state='牵手成功')
                        conn.commit()
                        return '恭喜你，牵手成功'
                    # 如果对方不满意
                    elif satisfaction == '满意' and record_message[0][7] != '满意':
                        conn.run_sql(
                            'update record set satisfaction_nv = "%s", satisfaction = "%s" where nanuser = "%s" and '
                            'nvuser '
                            '= "%s" ' % (
                                satisfaction, '不满意', dating_phone_dating, self.__phone_number))
                        conn.commit()
                        return '很遗憾，牵手失败'
                # 对方没评价
                else:
                    conn.run_sql(
                        'update record set satisfaction_nv = "%s" where nanuser = "%s" and nvuser = "%s" ' % (
                            satisfaction, dating_phone_dating, self.__phone_number))
                    conn.commit()
                    return '评价成功'
        except Exception as e:
            if 'conn' in dir():
                conn.rollback()
                print(e)
            return '错误，请联系工作人员'
        finally:
            if 'conn' in dir():
                conn.close_database('cursor')
                conn.close_database('connect')

    # 查询对自己满意人的信息
    def inquire_loev_me(self):
        try:
            conn = interact.interactSql()
            conn.connect_sql()
            conn.cursor_sql()
            conn.run_sql(
                'select * from user where good_impression1 = "%s" or good_impression2 = "%s" or good_impression3 '
                '= "%s"' % (self.__phone_number, self.__phone_number, self.__phone_number))
            return conn.get_results()
        except Exception as e:
            if 'conn' in dir():
                print(e)
            return '错误，请联系工作人员'
        finally:
            if 'conn' in dir():
                conn.close_database('cursor')
                conn.close_database('connect')
